package com.weitu.securitycheck.service.impl;

import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.securitycheck.entity.CheckProblem;
import com.weitu.securitycheck.entity.CheckProblemCriteria;
import com.weitu.securitycheck.mapper.CheckProblemMapper;
import com.weitu.securitycheck.service.CheckProblemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckProblemServiceImpl implements CheckProblemService {
    private final CheckProblemMapper checkProblemMapper;

    private static final Logger logger = LoggerFactory.getLogger(CheckProblemServiceImpl.class);

    @Autowired
    public CheckProblemServiceImpl(CheckProblemMapper checkProblemMapper) {
        this.checkProblemMapper = checkProblemMapper;
    }

    @Override
    public int countByCriteria(CheckProblemCriteria example) {
        int count = this.checkProblemMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public CheckProblem selectByPrimaryKey(String id) {
        return this.checkProblemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckProblem> selectByCriteria(CheckProblemCriteria example) {
        return this.checkProblemMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.checkProblemMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(CheckProblem record) {
        return this.checkProblemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(CheckProblem record, CheckProblemCriteria example) {
        return this.checkProblemMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(CheckProblem record) {
        return this.checkProblemMapper.insertSelective(record);
    }

    @Override
    @Transactional
    public boolean saveAccessoryAndInsert(CheckProblem checkProblem) {
        if (AccessoryUtil.saveAccerssoryInfo(checkProblem.getId())) {
            if (checkProblemMapper.insertSelective(checkProblem) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional
    public boolean saveAccessoryAndUpdate(CheckProblem checkProblem) {
        if (AccessoryUtil.saveAccerssoryInfo(checkProblem.getId())) {
            if (checkProblemMapper.updateByPrimaryKey(checkProblem) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<CheckProblem> selectByNotInRecord() {
        return this.checkProblemMapper.selectByNotInRecord();
    }

    @Override
    @Transactional
    public int updateByPrimaryKey(CheckProblem record) {
        return this.checkProblemMapper.updateByPrimaryKey(record);
    }
}